home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / faq / wdj0497.zip / TENNBERG.ZIP / CONVERT.C
C/C++ Source or Header  |  1997-01-17  |  982b  |  42 lines

  1. //
  2. // ConvertDataToHex() and ConvertHexToData() functions
  3. // 
  4.  
  5. #include <windows.h>
  6.  
  7. static LPSTR hexString = "0123456789ABCDEF";
  8. #define HexToInt(ch) \
  9.                 (BYTE)(((ch) > '9') ? ((ch) - 55) : ((ch) - '0'))
  10.  
  11. // Converts the data in source to a null-terminated hex
  12. // string in dest. The parameter size should contain 
  13. // the size of source.
  14. void ConvertDataToHex(const LPSTR source,LPSTR dest,int size)
  15.   {
  16.   int j = 0;
  17.  
  18.   for (int i = 0;i < size;i++)
  19.     {
  20.     BYTE ch = source[i];
  21.  
  22.     dest[j++] = hexString[(ch >> 4) & 0x0F];
  23.     dest[j++] = hexString[ch & 0x0F];
  24.     }
  25.   dest[j] = '\0';
  26.   }
  27.  
  28. // Converts the null-terminated hex string in source to
  29. // data stored in dest.
  30. void ConvertHexToData(const LPSTR source,LPSTR dest)
  31.   {
  32.   BYTE data;
  33.   int len = lstrlen(source),
  34.       j = 0;
  35.  
  36.   for (int i = 0;i < len;i+=2)
  37.     {
  38.     data = HexToInt(source[i]);
  39.     dest[j++] = (BYTE)((data << 4) | HexToInt(source[i+1]));
  40.     }
  41.   }
  42.